package com.educate.device.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import com.educate.device.dao.UpdatePackDao;
import com.educate.device.model.bo.UpdatePackBO;
import com.educate.device.model.po.UpdatePack;
import com.educate.device.rpc.api.UpdatePackService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class UpdatePackServiceImpl implements UpdatePackService {

    @Resource UpdatePackDao updatePackDao;

    @Override
    public void create(UpdatePackBO bo) {
        updatePackDao.insert(bo);
    }

    @Override
    public void delete(UpdatePackBO bo) {
        updatePackDao.delete(bo);
    }

    @Override
    public void delete(String updatePackId) {
        updatePackDao.deleteById(updatePackId);
    }

    @Override
    public UpdatePackBO detail(String updatePackId) {
        Optional<UpdatePack> optional = updatePackDao.findById(updatePackId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new UpdatePackBO(optional.get());
    }

    @Override
    public UpdatePackBO detail(UpdatePackBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(UpdatePackBO bo) {
        Optional<UpdatePack> optional = updatePackDao.findById(bo.getUpdatePackId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
        updatePackDao.save(optional.get());
    }

    @Override
    public Page<UpdatePackBO> query(UpdatePackBO bo, PageRequest pageRequest) {
        return updatePackDao.query(bo, pageRequest).map(UpdatePackBO::new);
    }

    @Override
    public List<UpdatePackBO> list(UpdatePackBO bo) {
        List<UpdatePack> result = updatePackDao.findAll();
        return ListUtils.map(result, UpdatePackBO::new);
    }
}
